package cn.cj.erp.common.utils.file;

import cn.hutool.core.util.ObjectUtil;
import cn.cj.erp.common.exception.ServiceException;
import org.apache.poi.util.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;

/**
 * 图片处理工具类
 *
 * @author ruoyi
 */
public class ImageUtil {
    private static final Logger log = LoggerFactory.getLogger(ImageUtil.class);

    /**
     * 获取文件类型
     *
     * @param photoByte 文件字节码
     * @return 后缀（不含".")
     */
    public static String getImageExtendName(byte[] photoByte) {
        String strFileExtendName = "JPG";
        if ((photoByte[0] == 71) && (photoByte[1] == 73) && (photoByte[2] == 70) && (photoByte[3] == 56)
                && ((photoByte[4] == 55) || (photoByte[4] == 57)) && (photoByte[5] == 97)) {
            strFileExtendName = "GIF";
        } else if ((photoByte[6] == 74) && (photoByte[7] == 70) && (photoByte[8] == 73) && (photoByte[9] == 70)) {
            strFileExtendName = "JPG";
        } else if ((photoByte[0] == 66) && (photoByte[1] == 77)) {
            strFileExtendName = "BMP";
        } else if ((photoByte[1] == 80) && (photoByte[2] == 78) && (photoByte[3] == 71)) {
            strFileExtendName = "PNG";
        }
        return strFileExtendName;
    }

    /**
     * 读取图片
     * @param imagePath 图片路径
     * @return byte[] 字节数组
     **/
    public static byte[] getImage(String imagePath) {
        InputStream is = getFile(imagePath);
        try {
            if (ObjectUtil.isNull(is)) {
                return null;
            }
            return IOUtils.toByteArray(is);
        } catch (Exception e) {
            log.error("图片读取异常\n", e);
            return null;
        } finally {
            IOUtils.closeQuietly(is);
        }
    }

    /**
     * 读取图片
     * @param imagePath 图片路径
     * @return java.io.InputStream 输入流
     **/
    public static InputStream getFile(String imagePath) {
        try {
            byte[] result = readFile(imagePath);
            if (ObjectUtil.isNull(result)) {
                return null;
            }
            result = Arrays.copyOf(result, result.length);
            return new ByteArrayInputStream(result);
        } catch (Exception e) {
            log.error("图片读取异常\n", e);
        }
        return null;
    }

    /**
     * 读取文件为字节数据
     * @param url 地址
     * @return 字节数据
     */
    public static byte[] readFile(String url) {
        InputStream in = null;
        try {
            if (url.startsWith("http")) {
                // 网络地址
                URL urlObj = new URL(url);
                URLConnection urlConnection = urlObj.openConnection();
                urlConnection.setConnectTimeout(30 * 1000);
                urlConnection.setReadTimeout(60 * 1000);
                urlConnection.setDoInput(true);
                in = urlConnection.getInputStream();
            } else {
                // 本机地址
                throw new ServiceException("不支持本机图片");
            }
            return IOUtils.toByteArray(in);
        } catch (Exception e) {
            log.error("获取文件路径异常", e);
            return null;
        } finally {
            IOUtils.closeQuietly(in);
        }
    }
}
